
import db.MYSQLControl;
import db.MyDataSource;
import driver.PhantomJsDriver;
import driver.chromedriver;
import model.model;
import org.apache.commons.dbutils.QueryRunner;
import org.openqa.selenium.*;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import parse.priceParse;
import url.url;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

public class test1 {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = null;
        DataSource ds = MyDataSource.getDataSource("jdbc:mysql://rm-wz9v9e07mh753tk321o.mysql.rds.aliyuncs.com:3306/car_spider");
        QueryRunner qr = new QueryRunner(ds);
        /*try {

            driver = chromedriver.getChromeDriver();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        driver = PhantomJsDriver.getPhantomJSDriver();
        driver.get("http://car.bitauto.com/tree_chexing/mb_9/");
        driver.manage().window().setSize(new Dimension(1000, 30000));
        driver.manage().window().maximize();
        //System.out.printf("now accesss %s \n", driver.getCurrentUrl());
        Thread.sleep(5000);
        List<WebElement> search_result = driver.findElements(By.xpath("//div[@class='brand-name']/span"));
        List<String> brand = new ArrayList();
        for(WebElement result : search_result){
            brand.add(result.getText());
        }
        String factory;
        String Car_big;
        for (String result : brand){
            (new WebDriverWait( driver, 10)) .until(
                    ExpectedConditions.presenceOfElementLocated(By.xpath(String.format("//span[text()='%s']",result)))
            ).click();
            List<WebElement> search_result1 = driver.findElements(By.xpath("//*[@id=\"divCsLevel_0\"]/h5"));
            if(search_result1.size()>0) {
                for (int i = 1; i < search_result1.size() + 1; i++) {
                    List<WebElement> search_result3 = driver.findElements(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div", i)));
                    for (int j = 1; j < search_result3.size() + 1; j++) {

                        // System.out.println(i+"aaa"+j+"mmm"+driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[1]",i,j))).getText());
                        List<WebElement> search_result4 = driver.findElements(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li", i, j)));


                        try {
                            qr.update(String.format("INSERT INTO car_price_1 VALUES ('%s','%s','%s','%s');", result, search_result1.get(i - 1).getText(), driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[1]", i, j))).getText(), driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[%d]", i, j, search_result4.size()))).getText()));
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }

                    }
                    Thread.sleep(1000);
                }
                Thread.sleep(1000);
            }else {
                List<WebElement> search_result3 = driver.findElements(By.xpath("//*[@id=\"divCsLevel_0\"]/div/div"));
                for (int j = 1; j < search_result3.size() + 1; j++) {

                    // System.out.println(i+"aaa"+j+"mmm"+driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[1]",i,j))).getText());
                    List<WebElement> search_result4 = driver.findElements(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div/div[%d]/div/ul/li", j)));


                    try {
                        qr.update(String.format("INSERT INTO car_price_1 VALUES ('%s','%s','%s','%s');", result, result, driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div/div[%d]/div/ul/li[1]",  j))).getText(), driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div/div[%d]/div/ul/li[%d]", j, search_result4.size()))).getText()));
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                }
                Thread.sleep(1000);

            }

        }
        driver.quit();
    }
}

